package com.cfcc.deptone.assetbarcodemgr.activity;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cfcc.deptone.assetbarcodemgr.R;
import com.cfcc.deptone.assetbarcodemgr.constant.AssetConstants;
import com.cfcc.deptone.assetbarcodemgr.entity.Product;
import com.cfcc.deptone.assetbarcodemgr.task.AssetListTask;
import com.cfcc.deptone.assetbarcodemgr.utils.PandianFileUtils;

public class AssetActivity extends AppCompatActivity {

    private static final String TAG = AssetActivity.class.getSimpleName();

    private ListView assetLv;
    private List assetList;
    private ProgressDialog pd;
    private ArrayAdapter adapter;
    private AssetListTask readTask;
    private TextView assetLvSummaryTv;
    private String panDianFlg;
    private String filePath;


    public TextView getAssetLvSummaryTv() {
        return assetLvSummaryTv;
    }

    public ListView getAssetLv() {
        return assetLv;
    }

    public List getAssetList() {
        return assetList;
    }

    public ProgressDialog getPd() {
        return pd;
    }

    public ArrayAdapter getAdapter() {
        return adapter;
    }

    /*private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case AssetConstants.ASSET_RECORD_READED:
                    if (adapter != null) {
                        adapter.notifyDataSetChanged();
                        Log.i(TAG, "list size:" + assetList.size());
                        for (int i = 0; i < assetList.size(); i++) {
                            Product product = (Product) assetList.get(i);
                            Log.i(TAG, "barcode=" + product.getBarCode());
                        }
                    }
                    break;
            }
        }

        ;
    };*/

    private final AssetLvRecvHandler searchRecvHandler = new AssetLvRecvHandler(this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asset);

        //取消按钮
        Button cancelBtn = (Button) findViewById(R.id.btn_assets_cancel);

        assetLv = (ListView) findViewById(R.id.asset_lv);
        assetLvSummaryTv = (TextView) findViewById(R.id.asset_lv_summary_tv);

        Intent intent = getIntent();
        final String des = intent.getExtras().getString(AssetConstants.PANDIAN_FLG);
        this.panDianFlg = des;
        //点击取消按钮回到主页面
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AssetActivity.this, MainActivity.class);
                startActivity(intent);

            }
        });

        //点击某一项迁移到详细页面
        assetLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Product product = (Product) assetLv.getItemAtPosition(position);
                new Thread() {
                    @Override
                    public void run() {

                        super.run();
                        //bufferRead();
                        //readRandom();
                        PandianFileUtils.searchRecord(searchRecvHandler, product.getBarCode(), getFilePath());
                    }


                }.start();
            }
        });

        //===============加入读取进度条=====================
        pd = new ProgressDialog(this);
        pd.setTitle("查询进度");

        pd.setMessage("正在查询中...");
        pd.setProgress(0);
        pd.setMax(AssetConstants.MAX_PROGRESS);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        //============================================


        readTask = new AssetListTask(this);
        if(AssetConstants.PANDIAN.equals(des)) {
            readTask.execute(des);
        } else if(AssetConstants.NO_PANDIAN.equals(des)){
            readTask.execute(AssetConstants.NO_PANDIAN, AssetConstants.INIT_PANDIAN);
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        readTask.cancel(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        searchRecvHandler.removeCallbacksAndMessages(null);
    }

    public void setAdapter(ArrayAdapter adapter) {
        this.adapter = adapter;
    }

    private static class AssetLvRecvHandler extends Handler {

        private final WeakReference<AssetActivity> mActivity;

        public AssetLvRecvHandler(AssetActivity activity) {
            mActivity = new WeakReference<AssetActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            AssetActivity _activity = mActivity.get();

            String desPandian = _activity.getPanDianFlg();

            Bundle searchRecodeBundle = msg.getData();
            Intent intent = new Intent(_activity, AssetDetailActivity.class);
            if(AssetConstants.PANDIAN.equals(desPandian)) {
                intent.putExtra(AssetConstants.SEARCHBYBARCODE, AssetConstants.FLG_PANDIAN_DETAIL);
            } else if(AssetConstants.NO_PANDIAN.equals(desPandian)) {
                intent.putExtra(AssetConstants.SEARCHBYBARCODE, AssetConstants.FLG_NO_PANDIAN_DETAIL);
            }
            intent.putExtra(AssetConstants.ASSET_RECORD, searchRecodeBundle);
            _activity.startActivity(intent);
        }
    }

    public String getPanDianFlg() {
        return panDianFlg;
    }

    public void setPanDianFlg(String panDianFlg) {
        this.panDianFlg = panDianFlg;
    }

    public String getFilePath() {
        filePath = AssetActivity.this.getFilesDir().getAbsoluteFile() + File.separator + AssetConstants.PANDIAN_TMPFILE_NAME;
        return filePath;
    }
}
